<?php echo $this->Html->script('app/permissions.js'); ?>

<div class="pageheader">
	<h1 class="pagetitle">Gerenciamento de permissões</h1>
	<span class="pagedesc"></span>
</div>
<div id="contentwrapper" class="contentwrapper">
	<div class="msghead_menu">
		<ul>
			<li><?php echo $this->Html->link(__('Gerenciar Permissões'), array('action' => 'permissions')); ?></li>
			<li><?php echo $this->Html->link(__('Atualizar Ações'), array('action' => 'update_acos')); ?></li>
		</ul>
	</div>
	</br></br></br>

	<div>
		<?php echo $this->Form->create('Perms'); ?>
			<div class="dataTables_wrapper">
				<table cellpadding="0" cellspacing="0" border="0" class="stdtable">
					<thead>
						<tr>
							<th>Ação</th>
							<?php foreach ($aros as $aro): ?>
							<?php $aro = array_shift($aro); ?>
							<th><?php echo h($aro[$aroDisplayField]); ?></th>
							<?php endforeach; ?>
						</tr>
					</thead>
					<tbody>
						<?php
						$uglyIdent = Configure::read('AclManager.uglyIdent'); 
						$lastIdent = null;
						foreach ($acos as $id => $aco) {
							$action = $aco['Action'];
							$alias = $aco['Aco']['alias'];
							$alias = __($alias);
							$ident = substr_count($action, '/');

							if (acaoInvisivel($alias)) {
								continue;
							}

							if ($ident <= $lastIdent && !is_null($lastIdent)) {
								for ($i = 0; $i <= ($lastIdent - $ident); $i++) {
									?></tr><?php
								}
							}
							if ($ident != $lastIdent) {
								$moduleClass = ($ident == 1) ? "module" : "";

								?><tr class='aclmanager-ident-<?php echo $ident; ?> <?php echo $moduleClass; ?>'><?php
							}
							?><td><?php echo ($ident == 1 ? "<strong>" : "" ) . ($uglyIdent ? str_repeat("&nbsp;&nbsp;", $ident) : "") . h($alias) . ($ident == 1 ? "</strong>" : "" ); ?></td>
							<?php foreach ($aros as $aro): 
								$inherit = $this->Form->value("Perms." . str_replace("/", ":", $action) . ".{$aroAlias}:{$aro[$aroAlias]['id']}-inherit");
								$allowed = $this->Form->value("Perms." . str_replace("/", ":", $action) . ".{$aroAlias}:{$aro[$aroAlias]['id']}"); 
								$value = $inherit ? 'inherit' : null; 
								$icon = $this->Html->image(($allowed ? 'test-pass-icon.png' : 'test-fail-icon.png')); ?>
								<td><?php echo $icon . " " . $this->Form->select("Perms." . str_replace("/", ":", $action) . ".{$aroAlias}:{$aro[$aroAlias]['id']}", array(array('inherit' => __('Herdar'), 'allow' => __('Permitir'), 'deny' => __('Negar'))), array('empty' => __('Nenhuma mudança'), 'value' => $value)); ?></td>
							<?php endforeach; ?>
						<?php 
							$lastIdent = $ident;
						}
						for ($i = 0; $i <= $lastIdent; $i++) {
							?></tr><?php
						}
						?>
					</tbody>
				</table>

				<div class="dataTables_info" id="dyntable_info"><?php echo $this->Paginator->counter(array('format' => __('Página %page% de %pages%, Mostrando de %start% até %end% de %count% registros'))); ?></div>
				<div class="dataTables_paginate paging_full_numbers" id="dyntable_paginate">
					<span class="previous paginate_button paginate_button_disabled" id="dyntable_previous"><?php echo $this->Paginator->prev( __('Anterior'), array(), null, array());?></span>
					<span class="next paginate_button paginate_button_disabled" id="dyntable_next"><?php echo $this->Paginator->next(__('Seguinte'), array(), null, array());?></span>
				</div>
			</div>

		</br>

		<?php echo $this->Form->end(__("Salvar")); ?>

		</br>
	</div>
</div>

<?php 
	function acaoInvisivel($alias) {
		$actions = array("login", "logout", "Pages", "display", "getRegra", "getGrupo", 
						 "crop", "getImage", "getDataUltimaChegada", "getDatasViagens",
						 "verificarVistos", "getByProjeto", "getByGrupo", "getByArea", 
						 "getByFuncionario", "getDiasFerias", "getBonusDescontoConversao");

		return in_array($alias, $actions);
	}
?>

